//
//  Data.swift
//  SwiftBase
//
//  Created by 郭艳波 on 2018/3/7.
//  Copyright © 2018年 郭艳波. All rights reserved.
//

import Foundation
import DefaultsKit

class CoreData {
    public static let sharedInstance = CoreData()
    private init() {}
    
    public static func getKey(key: String) -> String {
        return CoreConst.KEY_PREFIX + key
    }
    
    public static func get<T:Codable>(key: String) -> T? {
        let processKey = Key<T>(getKey(key: key))
        return Defaults().get(for: processKey)
    }
    
    public static func set<T:Codable>(key: String, value: T) {
        let processKey = Key<T>(getKey(key: key))
        Defaults().set(value, for: processKey)
    }
    
    public static func clear<T:Codable>(key: String, type: T) {
        let processKey = Key<T>(getKey(key: key))
        Defaults().clear(processKey)
    }
    
    public static func hasKey<T:Codable>(key: String, type: T) -> Bool {
        let processKey = Key<T>(getKey(key: key))
        if Defaults().has(processKey) {
            return true
        }
        
        return false
    }
    
    public static func getToken() -> String? {
        return get(key: CoreConst.KEY_TOKEN)
    }
    
    public static func setToken(value: String) {
        return set(key: CoreConst.KEY_TOKEN, value: value)
    }
    
    public static func getUser() -> User? {
        return get(key: CoreConst.KEY_USER)
    }
    
    public static func setUser(value: User) {
        return set(key: CoreConst.KEY_USER, value: value)
    }
}
